/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package etherblocks.game.entitysystem.systems;

import com.jme3.renderer.queue.RenderQueue;
import com.jme3.scene.Node;
import etherblocks.engine.objects.ModelObject;
import etherblocks.game.entitysystem.*;
import etherblocks.game.entitysystem.components.*;

/**
 *
 * @author Carl
 */
public class ModelSystem extends ModelObjectSystem{    

    public ModelSystem(Node rootNode){
        super(rootNode);
    }
    
    @Override
    public void onNextFrameCalculation(EntityMap entityMap, float lastTimePerFrame){
        for(Entity entity : entityMap.getEntities(VisualComponent.class)){
            VisualComponent visualComponent = entity.getComponent(VisualComponent.class);
            ModelObject modelObject = getModelObject(entity);
            if((modelObject != null) && (!modelObject.getUserData("modelName").equals(visualComponent.getModelName()))){
                rootNode.detachChild(modelObject);
                modelObject = null;
            }
            if(modelObject == null){
                modelObject = new ModelObject("/Models/" + visualComponent.getModelName() + "/skin.xml");
                modelObject.setName(getModelObjectName(entity.getID()));
                modelObject.setUserData("modelName", visualComponent.getModelName());
                modelObject.setShadowMode(RenderQueue.ShadowMode.CastAndReceive);
                rootNode.attachChild(modelObject);
            }
        }
    }
}
